﻿namespace AzureCamp.RssAggregatorBusiness
{
    using System;
    using System.Data.Services.Common;
    using System.Text;
    using System.IO;

    [DataServiceKey("PartitionKey", "RowKey")]
    public class RssLink : Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
    {
        private string url;

        public string Url
        {
            get
            {
                if (String.IsNullOrEmpty(this.url))
                {
                    return "unspecified_" + DateTime.Now.ToShortTimeString();
                }
                
                return this.url; 
            }
            set { this.url = value; }
        }
        private string feedHolder;

        public string FeedHolder
        {
            get {
                if(String.IsNullOrEmpty(this.feedHolder))
                {
                    return "unspecified_holder" + DateTime.Now.ToShortTimeString();
                }

                return this.feedHolder; }
            set { this.feedHolder = value; }
        }

        public RssLink() : base()
        {
            //this.Init();
        }

        public RssLink(string rowKey, string url)
        {
            this.PartitionKey = rowKey;
            this.FeedHolder = rowKey;
            this.Url = url;
            Init();
        }

        private void Init()
        {
            //this.RowKey = Guid.NewGuid().GetHashCode().ToString() + DateTime.Now.Ticks;
            this.RowKey = this.Url.Replace("/", "").
                Replace("?", "").
                Replace("\\", "").
                Replace("#", "").
                Replace(":", "").
                Replace("&", "").
                Replace("!", "").
                Replace("§", "").
                Replace("$", "").
                Replace("-", "");
            this.PartitionKey = this.PartitionKey.
                Replace("/", "").
                Replace("?", "").
                Replace("\\", "").
                Replace("#", "").
                Replace(":", "").
                Replace("&", "").
                Replace("!", "").
                Replace("§", "").
                Replace("$", "").
                Replace("-", "");
        }

    }
}
